package test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PlayCards {

    public static void main(String[] args) {
        int[] arr = {1,2,3,4,6,6,6,7,7,8,9,10,11,12,12,13,13,14,14};

        Map<Integer,Integer> cardsMap = new HashMap<>();
        for (int i = 0;i < arr.length ; i ++){
            if (cardsMap.keySet().contains(arr[i])){
                cardsMap.put(arr[i],cardsMap.get(arr[i]) + 1);
            }else {
                cardsMap.put(arr[i],1);
            }
        }
        for (Integer num : cardsMap.keySet()) {
            System.out.println(num + "  " + cardsMap.get(num));
        }

        // 定义容器保存结果
        List<Integer> resList = new ArrayList<>();
        // 定义记录改变的数组
        int[] memory = new int[arr.length];
        // 状态指针
        int num = 0;

        // 开始递归
        diGuiMethod(num,cardsMap,resList);
        // 输出
        System.out.println("Result = " + resList.get(0));

    }

    private static void diGuiMethod(int num, Map<Integer, Integer> cardsMap, List<Integer> resList) {

        // 获取状态列表
        List<Map<Integer, Integer>> nextState = getNextState(cardsMap);

        // 状态检查
        if (nextState.size() != 0){
            // 循环状态
            for (int i = 0;i < nextState.size() ;i ++){
                // 改变状态
                Map<Integer, Integer> temp = cardsMap;
                cardsMap = nextState.get(i);
                // 递归
                diGuiMethod(num + 1,cardsMap,resList);
                // 返回原状态
                cardsMap = temp;
            }
        }else {
            int count = 0;
            Map<Integer,Integer> temp = new HashMap<>(cardsMap);
            // 对子
            for (Integer key : temp.keySet()) {
                while (temp.get(key) >= 2){
                    temp.put(key,cardsMap.get(key) - 2);
                    count ++;
                }
            }
            // 单牌
            for (Integer key : temp.keySet()) {
                if (temp.get(key) > 0){
                    temp.put(key,temp.get(key) - 1);
                    count ++;
                }
            }
            if (!resList.isEmpty() && num + count < resList.get(0)){
                resList.set(0,num + count);
            }else {
                resList.add(num + count);
            }

            System.out.println(resList.get(0));
        }
    }

    private static List<Map<Integer, Integer>> getNextState(Map<Integer, Integer> cardsMap) {
        List<Map<Integer,Integer>> nextStateList = new ArrayList<>();
        // 顺子
        for (Integer key : cardsMap.keySet()) {
            if (cardsMap.get(key) >= 1){
                int count = 1;
                int keyTemp = key;
                while (cardsMap.containsKey(keyTemp + 1) && cardsMap.get(keyTemp + 1) >= 1){
                    count ++;
                    if (count >= 5){
                        Map<Integer,Integer> temp = new HashMap<>(cardsMap);
                        for (int i = key;i < key + count; i++){
                            temp.put(i,cardsMap.get(i) - 1);
                        }
                        nextStateList.add(temp);
                    }
                    keyTemp ++;
                }
            }
        }
        // 连对
        for (Integer key : cardsMap.keySet()) {
            if (cardsMap.get(key) >= 2){
                int count = 1;
                int keyTemp = key;
                while (cardsMap.containsKey(keyTemp + 1) && cardsMap.get(keyTemp + 1) >= 2){
                    count ++;
                    if (count >= 3){
                        Map<Integer,Integer> temp = new HashMap<>(cardsMap);
                        for (int i = key;i < key + count; i++){
                            temp.put(i,cardsMap.get(i) - 2);
                        }
                        nextStateList.add(temp);
                    }
                    keyTemp ++;
                }
            }
        }
        return nextStateList;
    }
}
